﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class SubjectInfo : System.Web.UI.Page
    {
        static int schoolid = Config.CurrentSchoolID;
        static int subjectid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var mySubject = from u in db.SubjectInfo
                                  select u;
                    this.Store2.DataSource = mySubject;
                    this.Store2.DataBind();
                }
            }
        }

        //protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        schoolid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myShool = from c in db.SchoolInfo
        //                      where c.SchoolID == schoolid
        //                      select c.FullName;
        //        foreach (var school in myShool)
        //        {
        //            DropDownField1.Text = school.ToString();
        //        }
        //        var mySubject = from u in db.SubjectInfo
        //                      where u.SchoolID==schoolid
        //                      select u;
        //        this.Store2.DataSource = mySubject;
        //        this.Store2.DataBind();
        //    }
        //    schoolid = 0;
        //}

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            //schoolid = 0;
            SubjectCode.Text = "";
            SubjectName.Text = "";
            this.FormWindow.Visible = true;
            this.FormWindow.Show();
            Button6.Show();
            Button7.Hide();
        }
        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.SubjectInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    subjectid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newsubject = from c in db.SubjectInfo
                                   where c.SubjectID==subjectid
                                   select c;
                    ExamAnalysis.Model.SubjectInfo newu = newsubject.First();
                    //var school = from s in db.SchoolInfo
                    //             where s.SchoolID == newu.SchoolID
                    //             select s.FullName;
                    //schoolid = newu.SchoolID;
                    SubjectCode.Text = newu.SubjectCode;
                    SubjectName.Text = newu.SubjectName;
                  //  DropDownField2.Text = school.First().ToString();
                }
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Hide();
                Button7.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个科目").Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.SubjectInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    subjectid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newsubject = from u in db.SubjectInfo
                                   where u.SubjectID == subjectid
                                   select u;
                    ExamAnalysis.Model.SubjectInfo newu = newsubject.First();
                    db.SubjectInfo.DeleteOnSubmit(newu);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("SubjectInfo.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个科目").Show();
            }
        }

        //protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        schoolid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myShool = from c in db.SchoolInfo
        //                      where c.SchoolID == schoolid
        //                      select c.FullName;
        //        foreach (var school in myShool)
        //        {
        //            DropDownField2.Text = school.ToString();
        //        }
        //    }
        //}

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            //if (DropDownField2.Text.ToString().Equals("请选择"))
            //{
            //    X.Msg.Alert("提示信息", String.Format("请选择一个学校")).Show();
            //}
            //else
            //{
                ExamAnalysis.Model.SubjectInfo newu = new ExamAnalysis.Model.SubjectInfo();
                newu.SubjectCode = SubjectCode.Text;
                newu.SubjectName = SubjectName.Text;
                newu.SchoolID = schoolid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    db.SubjectInfo.InsertOnSubmit(newu);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功添加科目")).Show();
                   // schoolid = 0;
                }
                Response.Redirect("SubjectInfo.aspx");
          //  }
        }

        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newsubject = from u in db.SubjectInfo
                               where u.SubjectID==subjectid
                               select u;
                ExamAnalysis.Model.SubjectInfo newu = newsubject.First();
                newu.SubjectCode = SubjectCode.Text;
                newu.SubjectName = SubjectName.Text;
                newu.SchoolID = schoolid;
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改科目信息")).Show();
                //schoolid = 0;
                subjectid = 0;
            }
            Response.Redirect("SubjectInfo.aspx");
        }
    }
}